package com.ymttest.business.service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.http.client.ClientProtocolException;

import com.ymatou.auth.parameter.DeviceAuthStatusRequestDtoBean;
import com.ymatou.auth.parameter.IpAreaAuthStatusRequestDtoBean;
import com.ymatou.auth.service.DeviceAuthStatusRequestDtoCall;
import com.ymatou.auth.service.IpAreaAuthStatusRequestDtoCall;
import com.ymatou.iapi.usersafe.parameter.*;
import com.ymatou.iapi.usersafe.service.*;
import com.ymt.core.tool.Logger;
import com.ymttest.database.model.NameList;
import com.ymttest.database.model.Userinfo;
import com.ymttest.database.sqlwapper.JUserWapper;
import com.ymttest.database.sqlwapper.UserSafeWapper;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.YMTDateUtil;

public class JUserSafe {
	/**
	 * 给userid用户设置安全问题
	 * @param userid
	 * @throws Exception
	 */
	public static List<SecurityQuestionAnswer> setSecurityQuestion(Integer userid) throws Exception{
		Logger.comment("设置安全问题，userid:"+userid);
		List<SecurityQuestionAnswer> securityQuestionAnswerList=new ArrayList<>();
		SecurityQuestionAnswer securityQuestionAnswer1=new SecurityQuestionAnswer();
		securityQuestionAnswer1.setQuestionId(1);
		securityQuestionAnswer1.setQuestionAnswer("测试1");
		securityQuestionAnswerList.add(securityQuestionAnswer1);
		
		SecurityQuestionAnswer securityQuestionAnswer2=new SecurityQuestionAnswer();
		securityQuestionAnswer2.setQuestionId(2);
		securityQuestionAnswer2.setQuestionAnswer("测试2");
		securityQuestionAnswerList.add(securityQuestionAnswer2);
		
		SecurityQuestionAnswer securityQuestionAnswer3=new SecurityQuestionAnswer();
		securityQuestionAnswer3.setQuestionId(3);
		securityQuestionAnswer3.setQuestionAnswer("测试3");
		securityQuestionAnswerList.add(securityQuestionAnswer3);
		
		SetSecurityQuestionBean setsecurityquestionBean=new SetSecurityQuestionBean();
		SetSecurityQuestionCall setsecurityquestionCall=new SetSecurityQuestionCall();
		
		setsecurityquestionBean.setUserId(userid);
		setsecurityquestionBean.setSecurityQuestionAnswerList(securityQuestionAnswerList);
		setsecurityquestionCall.setData(setsecurityquestionBean);
		setsecurityquestionCall.callService();			
		return securityQuestionAnswerList;
	} 
	public static RegistersafeCountBean initSafeCountBean() {
		RegistersafeCountBean rb=new RegistersafeCountBean();
		rb.setAppId("autotest");
		rb.setClientIp(TestDataManager.getRandomIp());
		rb.setDeviceId(TestDataManager.getRandomUUID().toString());
		rb.setLoginId("regsafetest_"+TestDataManager.getRandomNum(8));
		return rb;
	}
	/**
	 * 注册风控计数
	 * @param registersafecountBean
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static  void RegistersafeCount(RegistersafeCountBean registersafecountBean) throws ClientProtocolException, IOException {
		RegistersafeCountCall registersafecountCall=new RegistersafeCountCall();
		registersafecountCall.setData(registersafecountBean);
		registersafecountCall.callService();
	}
	/**
	 * 认证IP
	 * @param ip 登陆IP
	 * @param loginId 登录Id
	 * @param pwd 登陆密码
	 * @param sourceType 使用的终端（mainsite, main...）
	 * @throws IOException 
	 * @throws ClientProtocolException 
	 */
	public static void authIPArea(String ip, String loginId, String pwd, String sourceType) throws ClientProtocolException, IOException{
		IpAreaAuthStatusRequestDtoBean ipareaauthstatusrequestdtoBean=new IpAreaAuthStatusRequestDtoBean();
		IpAreaAuthStatusRequestDtoCall ipareaauthstatusrequestdtoCall=new IpAreaAuthStatusRequestDtoCall();
		ipareaauthstatusrequestdtoBean.setIp(ip);
		ipareaauthstatusrequestdtoBean.setLoginId(loginId);
		ipareaauthstatusrequestdtoBean.setPassword(pwd);
		ipareaauthstatusrequestdtoBean.setSuccess(true);
		ipareaauthstatusrequestdtoBean.setSource(sourceType);
		ipareaauthstatusrequestdtoCall.setData(ipareaauthstatusrequestdtoBean);
		ipareaauthstatusrequestdtoCall.callService();	
		Logger.verifyEquals("true", ipareaauthstatusrequestdtoCall.getString("Success"), "验证认证成功");
	}
	
	/**
	 * 设备认证
	 * @param clientId 设备clientId
	 * @param deviceId 设备Id
	 * @param loginId 登录Id
	 * @param pwd 登陆密码
	 * @param sourceType 登陆终端
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static void authApp(String clientId, String deviceId, String loginId, String pwd, String sourceType) throws ClientProtocolException, IOException{
		DeviceAuthStatusRequestDtoBean deviceauthstatusrequestdtoBean=new DeviceAuthStatusRequestDtoBean();
		DeviceAuthStatusRequestDtoCall deviceauthstatusrequestdtoCall=new DeviceAuthStatusRequestDtoCall();
		deviceauthstatusrequestdtoBean.setClientId(clientId);
		deviceauthstatusrequestdtoBean.setDeviceId(deviceId);
		deviceauthstatusrequestdtoBean.setIp(TestDataManager.getRandomIp());
		deviceauthstatusrequestdtoBean.setLoginId(loginId);
		deviceauthstatusrequestdtoBean.setPassword(pwd);
		deviceauthstatusrequestdtoBean.setSource(sourceType);
		deviceauthstatusrequestdtoBean.setSuccess(true);
		deviceauthstatusrequestdtoCall.setData(deviceauthstatusrequestdtoBean);
		deviceauthstatusrequestdtoCall.callService();
		Logger.verifyEquals("true", deviceauthstatusrequestdtoCall.getString("Success"), "验证认证成功");
	}
	/**
	 *  初始化数据库配置
	 *  DefaultSafeVerifyStrategy 默认是2开启,不受status影响
	 * @param status 风控状态 0 关闭 2开启
	 * @throws InterruptedException
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static void initConfig(int status) throws InterruptedException, ClientProtocolException, IOException {
		Logger.comment("初始化风控配置RiskStrategy.status:"+status);
		UserSafeWapper userSafeWapper = new UserSafeWapper();
		//ip注册风控
		userSafeWapper.updateRiskStrategyStatus(1, "IP");
		//设备号注册风控
		userSafeWapper.updateRiskStrategyStatus(1, "device");
		//ip设备号注册风控
		userSafeWapper.updateRiskStrategyStatus(1, "IP_device");
		//以上3个为注册策略不在VerifyRiskStrategy策略范围内,不修改状态
		
		//默认风控开关 dimension_strategy:checkOtherUserTypeLoginAuth=true
		userSafeWapper.updateRiskStrategyStatus(status, "DefaultSafeVerifyStrategy");
		//买家设备号黑名单 dimension_strategy:deviceIdAllowNull=true
		userSafeWapper.updateRiskStrategyStatus(status, "BuyerDeviceIdBlacklistVerifyStrategy");
		//买家ip区域认证(买家是否在此区域认证过)
		userSafeWapper.updateRiskStrategyStatus(status, "BuyerIpAreaVerifyStrategy");
		//买家ip黑名单列表(该ip是否在黑名单中) dimension_strategy:
//				loginSuccessLimit=5
//				loginSuccessLimitMinutes=2
//				loginFailLimit=3
//				loginFailLimitMinutes=1
//				lockExpiredMinutes=10
		userSafeWapper.updateRiskStrategyStatus(status, "BuyerIpBlacklistVerifyStrategy");
		//买家登入次数黑名单 dimension_strategy:
//				rangMinutes=1
//				countLimit=5
		userSafeWapper.updateRiskStrategyStatus(status, "BuyerLoginCountVerifyStrategy");
		//买家登入密码错误次数 dimension_strategy:
//				rangMinutes=1
//				countLimit=5
		userSafeWapper.updateRiskStrategyStatus(status, "BuyerLoginPasswordFailTimesVerifyStrategy");
		//买家常用设备号认证  dimension_strategy:deviceIdAllowNull=true
		userSafeWapper.updateRiskStrategyStatus(status, "BuyerOftenDeviceIdVerifyStrategy");
		//买家token认证
		userSafeWapper.updateRiskStrategyStatus(status, "BuyerTokenVerifyStrategy");
		//买手设备号黑名单  dimension_strategy:deviceIdAllowNull=true
		userSafeWapper.updateRiskStrategyStatus(status, "SellerDeviceIdBlacklistVerifyStrategy");
		//买手ip区域认证
		userSafeWapper.updateRiskStrategyStatus(status, "SellerIpAreaVerifyStrategy");
		//买手ip黑名单 dimension_strategy:
//				loginSuccessLimit=5
//				loginSuccessLimitMinutes=2
//				loginFailLimit=3
//				loginFailLimitMinutes=1
//				lockExpiredMinutes=10
		userSafeWapper.updateRiskStrategyStatus(status, "SellerIpBlacklistVerifyStrategy");
		//买手登入次数 dimension_strategy:
//				rangMinutes=1
//				countLimit=5
		userSafeWapper.updateRiskStrategyStatus(status, "SellerLoginCountVerifyStrategy");
		//买手登入密码错误次数 dimension_strategy:
//		rangMinutes=1
//		countLimit=5
		userSafeWapper.updateRiskStrategyStatus(status, "SellerLoginPasswordFailTimesVerifyStrategy");
		//买手常用设备号验证 dimension_strategy:deviceIdAllowNull=true
		userSafeWapper.updateRiskStrategyStatus(status, "SellerOftenDeviceIdVerifyStrategy");
		//买手token验证
		userSafeWapper.updateRiskStrategyStatus(status, "SellerTokenVerifyStrategy");
		//买手登入id黑名单
		userSafeWapper.updateRiskStrategyStatus(status, "SellerLoginIdBlackListVerifyStrategy");
		//买家登入id黑名单
		userSafeWapper.updateRiskStrategyStatus(status, "BuyerLoginIdBlackListVerifyStrategy");
	}
	public static void initBlacklistConfig() throws InterruptedException, ClientProtocolException, IOException {
		UserSafeWapper userSafeWapper = new UserSafeWapper();

		// 默认风控开关 dimension_strategy:checkOtherUserTypeLoginAuth=true
		userSafeWapper.updateRiskStrategyStatus(0, "DefaultSafeVerifyStrategy");
		// 买家设备号黑名单 dimension_strategy:deviceIdAllowNull=true
		userSafeWapper.updateRiskStrategyStatus(2, "BuyerDeviceIdBlacklistVerifyStrategy");
		// 买家ip区域认证(买家是否在此区域认证过)
		userSafeWapper.updateRiskStrategyStatus(0, "BuyerIpAreaVerifyStrategy");
		// 买家ip黑名单列表(该ip是否在黑名单中) dimension_strategy:
		// loginSuccessLimit=5
		// loginSuccessLimitMinutes=2
		// loginFailLimit=3
		// loginFailLimitMinutes=1
		// lockExpiredMinutes=10
		userSafeWapper.updateRiskStrategyStatus(2, "BuyerIpBlacklistVerifyStrategy");
		// 买家登入次数黑名单 dimension_strategy:
		// rangMinutes=1
		// countLimit=5
		userSafeWapper.updateRiskStrategyStatus(0, "BuyerLoginCountVerifyStrategy");
		// 买家登入密码错误次数 dimension_strategy:
		// rangMinutes=1
		// countLimit=5
		userSafeWapper.updateRiskStrategyStatus(0, "BuyerLoginPasswordFailTimesVerifyStrategy");
		// 买家常用设备号认证 dimension_strategy:deviceIdAllowNull=true
		userSafeWapper.updateRiskStrategyStatus(0, "BuyerOftenDeviceIdVerifyStrategy");
		// 买家token认证
		userSafeWapper.updateRiskStrategyStatus(0, "BuyerTokenVerifyStrategy");
		// 买手设备号黑名单 dimension_strategy:deviceIdAllowNull=true
		userSafeWapper.updateRiskStrategyStatus(2, "SellerDeviceIdBlacklistVerifyStrategy");
		// 买手ip区域认证
		userSafeWapper.updateRiskStrategyStatus(0, "SellerIpAreaVerifyStrategy");
		// 买手ip黑名单 dimension_strategy:
		// loginSuccessLimit=5
		// loginSuccessLimitMinutes=2
		// loginFailLimit=3
		// loginFailLimitMinutes=1
		// lockExpiredMinutes=10
		userSafeWapper.updateRiskStrategyStatus(2, "SellerIpBlacklistVerifyStrategy");
		// 买手登入次数 dimension_strategy:
		// rangMinutes=1
		// countLimit=5
		userSafeWapper.updateRiskStrategyStatus(0, "SellerLoginCountVerifyStrategy");
		// 买手登入密码错误次数 dimension_strategy:
		// rangMinutes=1
		// countLimit=5
		userSafeWapper.updateRiskStrategyStatus(0, "SellerLoginPasswordFailTimesVerifyStrategy");
		// 买手常用设备号验证 dimension_strategy:deviceIdAllowNull=true
		userSafeWapper.updateRiskStrategyStatus(0, "SellerOftenDeviceIdVerifyStrategy");
		// 买手token验证
		userSafeWapper.updateRiskStrategyStatus(0, "SellerTokenVerifyStrategy");
		// 买手登录ID黑名单
		userSafeWapper.updateRiskStrategyStatus(2, "SellerLoginIdBlackListVerifyStrategy");
		// 买家登录ID黑名单
		userSafeWapper.updateRiskStrategyStatus(2, "BuyerLoginIdBlackListVerifyStrategy");
	}
	/**
	 * 插入新风控规则 默认生效1天<br>
	 * Name varchar 150 否 名称 ip 或 设备号等，指名称类型中所对应的业务值<br>
	 * ListType int 11 否 名单类型 如：：1：白名单，2：黑名单<br>
	 * NameType int 11 否 名称类型 如：1：用户编号，2：登录名，3：IP ，4：设备号<br>
	 * ScopeType int 11 否 作用域类型 如：0： 全局范围，1：买家，2：买手<br>
	 * 
	 * @param namelist
	 * @return
	 */
	public static  void insertNameList(int ListType, int NameType, int ScopeType, String name) {
		UserSafeWapper wapper = new UserSafeWapper();
		NameList namelist = new NameList();
		namelist.setCreateTime(YMTDateUtil.getDate());
		namelist.setStartTime(YMTDateUtil.getDate(YMTDateUtil.getBeforeOrNextDay(-1, YMTDateUtil.YMDTHMSS_STRING), YMTDateUtil.YMDTHMSS_STRING));
		namelist.setEndTime(YMTDateUtil.getDate(YMTDateUtil.getBeforeOrNextDay(1, YMTDateUtil.YMDTHMSS_STRING), YMTDateUtil.YMDTHMSS_STRING));
		namelist.setLastUpdateTime(YMTDateUtil.getDate());
		namelist.setCreator("autotest");
		namelist.setName(name);
		namelist.setNameType(NameType);
		namelist.setScopeType(ScopeType);
		namelist.setListType(ListType);
		wapper.insertNameList(namelist);
	}

	/**
	 * 插入新风控规则 <br>
	 * Id int 11 否 主键 <br>
	 * Name varchar 150 否 名称 ip 或 设备号等，指名称类型中所对应的业务值<br>
	 * ListType int 11 否 名单类型 如：：1：白名单，2：黑名单<br>
	 * NameType int 11 否 名称类型 如：1：用户编号，2：登录名，3：IP ，4：设备号<br>
	 * ScopeType int 11 否 作用域类型 如：0： 全局范围，1：买家，2：买手<br>
	 * StartTime datetime 0 否 有效期开始时间 <br>
	 * EndTime datetime 0 否 有效期结束时间 <br>
	 * CreateTime datetime 0 否 记录创建时间 <br>
	 * LastUpdateTime datetime 0 否 记录最后一次更新时间 <br>
	 * Remark varchar 1000 是 备注 <br>
	 * Creator varchar 50 否 创建者<br>
	 * 
	 * @param namelist
	 * @return
	 */
	public static  void insertNameList(int ListType, int NameType, int ScopeType, String name, Date starttime, Date endtime) {
		UserSafeWapper wapper = new UserSafeWapper();
		NameList namelist = new NameList();
		namelist.setCreateTime(YMTDateUtil.getDate());
		namelist.setStartTime(starttime);
		namelist.setEndTime(endtime);
		namelist.setLastUpdateTime(YMTDateUtil.getDate());
		namelist.setCreator("autotest");
		namelist.setName(name);
		namelist.setNameType(NameType);
		namelist.setScopeType(ScopeType);
		namelist.setListType(ListType);
		wapper.insertNameList(namelist);
	}
	/**
	 * 把用户id 设置黑名单
	 * @param uid
	 */
	public static void setIpRisk(int uid){
		UserSafeWapper usw = new UserSafeWapper();
		usw.deleteIPAreaData(uid, null, null, null);
		String dateTime = YMTDateUtil.getBeforeOrNextDay(0);
		usw.insertUserIpAuth(uid, "*", "*", "0", 0, dateTime, dateTime, "autoTest", dateTime, dateTime);
		 Userinfo uinfo = new JUserWapper().selectUserinfoByUid(uid).get(0);
		insertNameList(2, 2, 0, uinfo.getLoginId());
		insertNameList(2, 2, 0, uinfo.getLoginMobile());
	}
}
